﻿using Assets.Scripts.DL.Consts;
using Assets.Scripts.DL.Mgrs;
using UnityEngine;

public class PlayerTick : PlayerBaseState {
	readonly float _speed;
	bool _atk1DmgSend;
	bool _secondAnimStart;
	bool _atk2DmgSend;
	public PlayerTick(PlayerController controller, PlayerStateEnum stateName, State<PlayerStateEnum> superFSM, StateFactory<PlayerStateEnum> factory) : base(controller, stateName, superFSM, factory) {
		AddConditionToSelf(() => _controller.TickInput);

		_speed = _controller.ConfigData.GrdAtkXmoveSpeed;
	}

	public override void Enter() {
		AudioManager.Play(_controller.StateAudio, AudioStr.TickAduio);
		_controller.PlayAnim(PlayerStateEnum.GroundTick1);
		_secondAnimStart = false;
		_atk1DmgSend = false;
		_atk2DmgSend = false;
	}
	public override void Update() {
		base.Update();
		var v = _controller.Velocity;
		_controller.Velocity = new Vector2(_controller.XInput * _speed, v.y);

		if (!_atk1DmgSend) {
			CheckDamage(ref _atk1DmgSend);
		}
		var tmp = true;
		if (_atk1DmgSend && _controller.TickInput && !_secondAnimStart) {
			_controller.PlayAnim(PlayerStateEnum.GroundTick2);
			_secondAnimStart = true;
			tmp = false;
		}
		if (_secondAnimStart && tmp && !_atk2DmgSend) {
			CheckDamage(ref _atk2DmgSend);
		}
	}

	public override void Exit() {
	}

	void CheckDamage(ref bool atkmark) {
		if (_controller.IsAnimPlayAt(0.65f)) {
			//Debug.Log("SendDamage");
			_controller.CheckAtk(_controller.ConfigData.AirTickOff, _controller.ConfigData.GrdTickRad, _controller.ConfigData.TickDamage);
			atkmark = true;
		}
	}
}